--[[
    对局详情界面
]]--

local blaze  = require "blaze"
local app = require "app"
local game = require "club.threecard.sudoku"

local M = game.ui.activity()

M.store_watchers = {
    async_game = {
        ["$"] = blaze.noop,
        ["更新对局列表"] = function(self, store, state, args)
            local selected = game.store.versus.state.selected
            if not selected then
                return
            end
            local versus = state.versus_map[selected.id]
            if not versus then
                return
            end
            game.store.versus.select(versus)
        end
    },
    versus = {
        ["$"] = function(self, store, state, args)
            self.win_txt.text = "0"
            self.draw_txt.text = "0"
            self.lose_txt.text = "0"
        end,
        ["选中"] = function(self, store, state, args)
            local my_user_id = game.kbe.player().user_id
            local result = game.util.versus.analyze(state.selected, my_user_id)
            local win = result.win
            local draw = result.draw
            local lose = result.lose
            local me = result.me
            local target = result.target
            local sessions = state.selected.sessions
            
            self.win_txt.text = tostring(win)
            self.draw_txt.text = tostring(draw)
            self.lose_txt.text = tostring(lose)
            self.my_name_txt.text = me.nickname
            self.my_avatar_icon:set_head(me.head)
            self.target_avatar_icon:set_head(target.head)
            self.target_name_txt.text = target.nickname
            self.session_list:fill(sessions)
        end
    }
}

function M:ctor()
    local SimpleList = require "blaze.ui.widget.SimpleList"
    local DetailListItem = require "club.threecard.sudoku.ui.versus.DetailListItem"

    self.res = "club.threecard.sudoku/ui/versus.b:DetailUI"
    self.session_list = SimpleList.new()
    self.session_list.create_item = function(_, data, index)
        return DetailListItem.new(game.store.versus.state.selected, data, index)
    end
    local AvatarIcon = require "ui.Common.AvatarIcon"
    self.my_avatar_icon = AvatarIcon.new()
    self.target_avatar_icon = AvatarIcon.new()
end

function M:on_load()
    self:super("on_load")
    self.session_list:load()
end

function M:on_injected(bind)
    bind("button.click", self.back_btn, self.on_back_click)
    bind("shortcut.escape", self.back_btn.gameObject, self.on_back_click)
    self.session_list:set_parent(self.session_list_root)
    self.my_avatar_icon:set_gameobject(self.my_avatar_icon_obj, true)
    self.target_avatar_icon:set_gameobject(self.target_avatar_icon_obj, true)
end

function M:after_route_enter(to, from)
    local route = app.router.current()
    local versus = game.store.async_game.state.versus_map[route.params.id]
    if not versus then
        print("[TODO]对局不存在，返回")
        game.router.back()
        return
    end
    self:super("after_route_enter", to, from)
    game.store.versus.select(versus)
end

function M:on_back_click()
    game.audio.post("click")
    game.router.back()
end

function M:on_release()
    self.session_list:release()
    self.my_avatar_icon:release()
    self.target_avatar_icon:release()
end

return M